// Copyright (C) joyinter
// All rights reserved.
// 
// Author : Jiang Li
// Date   : 2005-01-25

#include "stdafx.h"
#include <common/svr_base/svr_showinfo.h>
#include "msvr_server.h"
#include "msvr_room.h"
#include "msvr_user.h"
#include "dbmsg_mainhandler.h"

namespace JoyServer
{

void CJoyDbMsgMainHandler::OnDbMsg_RoomNote(const CJoyDbMsg& msg)
{
    CJoyXmlElement* root = msg.m_root;
    if(root == NULL)
    {
        m_server->m_showInfo->QueueTimeColorText(InfoColor_Error,
            "bad room note format. <%s>.\n", (PCSTR) msg.m_content);
        return;
    }

    CJoyMainServer* pServer = GetServer();

    CArray<CJoyMainRoom*> rooms;
    UINT roomId = root->ReadIntD("room_id", 0);
    if(roomId == 0)
    {
        CJoyAutoCS gate(pServer->m_userSetsLock);
        POSITION pos = pServer->m_rooms.GetStartPosition();
        while(pos)
        {
            CJoyMainRoom* pRoom = NULL;
            UINT rid = 0;
            pServer->m_rooms.GetNextAssoc(pos, rid, pRoom);
            rooms.Add(pRoom);
        }
    }
    else
    {
        CJoyMainRoom* pRoom = NULL;
        CJoyAutoCS gate(pServer->m_userSetsLock);
        if(!pServer->m_rooms.Lookup(roomId, pRoom))
        {
            return;
        }
        rooms.Add(pRoom);
    }
    
    int c = (int)rooms.GetCount();
    for(int i = 0; i < c; ++ i)
    {
        CJoyMainRoom* pRoom = rooms[i];
        CJoyAutoCS gate(pRoom->m_userLock);
        pRoom->BroadcastNote(msg.m_content, msg.m_content.GetLength());
    }
}


} // namespace JoyServer